<?php

function workflow_action_list_page(){
  global $user;
  $output = '';

  if(!function_exists('get_swimlanes_by_role')){
    return '';
  }
  $userSwimlaneArray = get_swimlanes_by_role($user);//print_r($userSwimlaneArray);
  $json_array = array();
  $json_array['userName'] = $user->name;
  $json_array['userSwimlaneKeys'] = implode(",", $userSwimlaneArray);
  //$json_array['userSwimlaneKeys'] = 'ComplexInitiationWorkflow3_HRLevel2';
  $json_array['systemCode'] = SYSTEM_CODE;
  $json_array['moduleCode'] = "if";
  $json_array['uniqueCode'] = '';
  
  
  $json = json_encode($json_array); //print_r($json);die;
  $service = WORKFLOW_SERVICE_API.'getPendingHandingActionList.ws';
  
  //module_load_include('inc', 'webform_workflow','webform_workflow.service');
  //$api_result = call_workflow_service_api($service,$json); //print_r($api_result);die;
  /**/
  //-----------------hard code -------------
  $api_result['body'] = <<<EOD
  {
	"ResponseStatusInfo" : 
	{
		"StatusMessage" : "getting pending  handing task list is completing without errors.",
		"StatusCode" : 200
	},
	"pengingHandlingTaskList" : 
	[
		{
			"taskId" : "1328",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359688878657,
			"taskName" : "Submitted",
			"owner" : "",
			"privateProcessDefId" : "36",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "324324324234324324",
			"processInstanceId" : "1301",
			"statusCode" : "Submitted"
		},
		{
			"taskId" : "1352",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359688992613,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "36",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "324324324234324324",
			"processInstanceId" : "1344",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "1362",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359689055333,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "36",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "324324324234324324",
			"processInstanceId" : "1354",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "1412",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359696161673,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "36",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "324324324234324324",
			"processInstanceId" : "1404",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "1525",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359696331337,
			"taskName" : "Submitted",
			"owner" : "",
			"privateProcessDefId" : "36",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "324324324234324324",
			"processInstanceId" : "1501",
			"statusCode" : "Submitted"
		},
		{
			"taskId" : "1609",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359698998977,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "36",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "324324324234324324",
			"processInstanceId" : "1601",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "1639",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359699403470,
			"taskName" : "Submitted",
			"owner" : "",
			"privateProcessDefId" : "36",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "324324324234324324",
			"processInstanceId" : "1378",
			"statusCode" : "Submitted"
		},
		{
			"taskId" : "1845",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359708304327,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "36",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "324324324234324324",
			"processInstanceId" : "1837",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "1914",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359707990997,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "69",
			"systemCode" : "",
			"uniqueCode" : "profile1359707989991",
			"processInstanceId" : "1906",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "2014",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359708074717,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "76",
			"systemCode" : "",
			"uniqueCode" : "profile1359708074076",
			"processInstanceId" : "2006",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "2114",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359708097313,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "83",
			"systemCode" : "",
			"uniqueCode" : "profile1359708096652",
			"processInstanceId" : "2106",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "2214",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359708240277,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "91",
			"systemCode" : "",
			"uniqueCode" : "profile1359708239043",
			"processInstanceId" : "2206",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "2314",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359708384190,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "99",
			"systemCode" : "",
			"uniqueCode" : "profile1359708379598",
			"processInstanceId" : "2306",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "2438",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359945976773,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "36",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "324324324234324324",
			"processInstanceId" : "2430",
			"statusCode" : "Draft"
		},
		{
			"taskId" : "528",
			"overTime" : null,
			"candidateUsers" : "",
			"swimlaneKeys" : "BasicInitiationWorkflow1_HRLevel1",
			"assignee" : "",
			"startTime" : 1359617949113,
			"taskName" : "Draft",
			"owner" : "",
			"privateProcessDefId" : "20",
			"systemCode" : "TestSystem1",
			"uniqueCode" : "TestSystem1.TestModule.BasicInitiationWorkflow1",
			"processInstanceId" : "353",
			"statusCode" : ""
		}
	]
}
EOD;
  //-----------------hard code -------------

  $result = json_decode($api_result['body'],true); //print_r($result);die;
  
  //is return success
  drupal_add_js(drupal_get_path('module', 'relo_sitemanage') .'/js/sitemanage.extra.js');
  //drupal_add_css('#workflow_action_list li{display:none;}', array('type' => 'inline'));
  
  $return_code = $result['ResponseStatusInfo']['StatusCode'];
  if($return_code == 200){
    $pagesize = variable_get('BB_ST_ACTION_LIST_PAGESIZE','5');
    $n = 0;
    $output .= '<div id="workflow_action_list"><ul>';
    foreach ($result['pengingHandlingTaskList'] as $list){
      $page = (int)($n/$pagesize); //Current
      //$output .= '<li page="'.$page.'">';
      
      
      //call function from the $list['processInstanceId'] get $sid $nid
      //$list['processInstanceId'];
      //$nid = 1093 ;

      $instance_id = $list['processInstanceId'] ;
      $wf_private_process_def_id = $list['privateProcessDefId'] ;
      //----------------get entity_id---------------
      $query = db_select('workflow_instance_entity_mapping', 'em', array('fetch' => PDO::FETCH_ASSOC))
        ->fields('em', array('entity_id'))
        ->condition('em.instance_id', $list['processInstanceId'])
        ->condition('em.wf_private_process_def_id', $wf_private_process_def_id);
      $sid = $query->execute()->fetchColumn();
      //-------------end get entity_id---------------
      
      
      $query = db_select('webform_submissions', 's', array('fetch' => PDO::FETCH_ASSOC))
        ->fields('s', array('nid'))
        ->condition('s.sid', $sid);
      $nid = $query->execute()->fetchColumn();//print_r($nid);die;
      
      //hard code == workflow api
      module_load_include('inc','webform','includes/webform.submissions');
      $submission = webform_get_submission($nid, $sid);
      $submission_data = $submission->data;
      $first = isset($submission_data['11']['value'][0]) ? $submission_data['11']['value'][0] : '';
      $last = isset($submission_data['12']['value'][0]) ? ', '.$submission_data['12']['value'][0] : '';
      $name = $first.$last ; //print_r($name);die;
      //end hard

      $overTime = ceil($list['overTime']/1000); //echo $overTime;
      $overDate = t('Expired date is:').format_date($overTime, 'large');
      $expiration_day = variable_get('BB_ST_ACTION_LIST_EXPIRATION_DAY','5');
      $expiration_time = $expiration_day*24*3600 ;
      $currentTime = REQUEST_TIME ;
      if($currentTime>=$overTime){
        $expired = 'red'; //Expired
      }else if($currentTime>=$overTime-$expiration_time){
        $expired = 'yellow'; //Expired
      }else{
        $expired = 'green'; //Expired
      }
      
      $rows = array();
      if ($sid) {
        /*
        $output .= '<li page="' . $page . '" class="'.$expired.'" >';
        $the_url = url('node/' . $nid . '/submission/' . $sid, array('query' => array('actionId' => $list['taskId'])));
        $output .= '<a href="' . $the_url . '" title="'.$overDate.'">'.$name.'\'s Initiation Form requires your action. </a>';
        $output .= '</li>';
        */
        $row['expired'] = $expired;
        $row['expired'] = $expired;
        $row['expired'] = $expired;
        $rows[] = $row;
        $n++;
      }
    }
    
    
    
$count = $row['count'];
if( $count >0 ) {
	$total_pages = ceil($count/$limit);
} else {
	$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)

$page = 1;
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
foreach ($rows as $i => $row) {
    $responce->rows[$i]['id']=$row[id];
    $responce->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
    $i++;
}
echo json_encode($responce);





  }//end if return_code
 
  
  $search = drupal_get_form('search_action_list');
  
  return drupal_render($search).$output;
}

function search_action_list($form, $form_state) {
    $form['actions']['submit'] = array(
        '#type' => 'Search',
        '#value' => t('Continue'),
    );
    return $form;
}
